package study.edumanagesystemspringboot.config;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * Author：zouran
 * Date：2023/3/30  14:01
 * Description: Standard API
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@Component
public class ResultMap<T> {
    private int code;
    private String message;
    private T data;

    public <T> ResultMap<T> success(T t) {
        ResultMap<T> resultMap = new ResultMap<>();
        resultMap.setCode(CodeEnum.SUCCESS.getCode());
        resultMap.setMessage(CodeEnum.SUCCESS.getMessage());
        resultMap.setData(t);
        return resultMap;
    }

    public <T> ResultMap<T> successWithMessage(String message) {
        ResultMap<T> resultMap = new ResultMap<>();
        resultMap.setCode(CodeEnum.SUCCESS.getCode());
        resultMap.setMessage(message);
        resultMap.setData(null);
        return resultMap;
    }

    public <T> ResultMap<T> errorEarthquakeId() {
        ResultMap<T> resultMap = new ResultMap<>();
        resultMap.setCode(CodeEnum.ERROR_EARTHQUAKEID.getCode());
        resultMap.setMessage(CodeEnum.ERROR_EARTHQUAKEID.getMessage());
        resultMap.setData(null);
        return resultMap;
    }

    public <T> ResultMap<T> error() {
        ResultMap<T> resultMap = new ResultMap<>();
        resultMap.setCode(CodeEnum.ERROR_SERVER.getCode());
        resultMap.setMessage(CodeEnum.ERROR_SERVER.getMessage());
        resultMap.setData(null);
        return resultMap;
    }

    public <T> ResultMap<T> errorPath() {
        ResultMap<T> resultMap = new ResultMap<>();
        resultMap.setCode(CodeEnum.ERROR_PATH.getCode());
        resultMap.setMessage(CodeEnum.ERROR_PATH.getMessage());
        resultMap.setData(null);
        return resultMap;
    }

    public <T> ResultMap<T> errorParams() {
        ResultMap<T> resultMap = new ResultMap<>();
        resultMap.setCode(CodeEnum.ERROR_PARAM.getCode());
        resultMap.setMessage(CodeEnum.ERROR_PARAM.getMessage());
        resultMap.setData(null);
        return resultMap;
    }

    public <T> ResultMap<T> errorParamsWithMessage(String message) {
        ResultMap<T> resultMap = new ResultMap<>();
        resultMap.setCode(400);
        resultMap.setMessage(message);
        resultMap.setData(null);
        return resultMap;
    }

    public <T> ResultMap<T> custom(int code, String message, T t) {
        ResultMap<T> resultMap = new ResultMap<>();
        resultMap.setCode(code);
        resultMap.setMessage(message);
        resultMap.setData(t);
        return resultMap;
    }

    public <T> ResultMap<T> errorWithMessage(String message) {
        ResultMap<T> resultMap = new ResultMap<>();
        resultMap.setCode(500);
        resultMap.setMessage(message);
        resultMap.setData(null);
        return resultMap;
    }

    public <T> ResultMap<T> codeAndMessage(int code, String message) {
        ResultMap<T> resultMap = new ResultMap<>();
        resultMap.setCode(code);
        resultMap.setMessage(message);
        resultMap.setData(null);
        return resultMap;
    }

}
